// pages/index.js
import { IndexModel } from "../../models/index.js";
import { UserModel } from "../../models/user.js";
const indexModel = new IndexModel();
const userModel = new UserModel();
import {
    promisic
  } from '../../util/common.js'
const dateUtils = require('../../util/dateutils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
   selTypeMsg:"",
   selTypeId:0,
   errorCount:0,
   noteCount:0,
   coolectCount:0,
   examTime:-1,
   unitList:null,
   setTimeShow:false,
   dateComponentData: {type: "YMD"},
   dateComponentDate: new Date().getTime(),
   allTopicCount:0,
   userDoCount:0,
   loginShow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  async loadData(isSet,courseId) {
    const data = await indexModel.indexUserDta(isSet,courseId);
    if(data==-1){
        //没登录
        this.setData({
            loginShow:true
        });
        return;
    }
    this.setData({
        coolectCount:data.coolectCount,
        selTypeMsg:data.selTypeMsg,
        selTypeId:data.courseId,
        errorCount:data.errorCount,
        noteCount:data.noteCount,
        examTime:data.examTime,
        unitList:data.unitList,
        allTopicCount:data.allTopicCount,
        userDoCount:data.userDoCount
    });
    if(isSet==0&&this.data.selTypeMsg==""){
        wx.navigateTo({
            url: "/pages/sel_type_list/index",
          });
      }
  },
  setTime:function(params) {
      const operType=params.currentTarget.dataset.type;
      let setTimeShow=false;
      if(operType==1){
        setTimeShow=true;
      }
      this.setData({
        setTimeShow:setTimeShow
      });
  },
  async datePickerOkEvent(e) {
    let date = e.detail.date;
    //更新到数据库
    const data = await indexModel.setExamTime(dateUtils.formatLongTime(date, "Y-M-D"));
    this.setData({
        setTimeShow:false,
        examTime: data
    });
  },
  selType:function (params) {
    wx.navigateTo({
        url: "/pages/sel_type_list/index",
      });
  },
  //用户登录
  async userLogin(){
    const res = await promisic(wx.getSetting)();
    var that = this;
    if (!res.authSetting['scope.userInfo']) {
      await promisic(wx.authorize)({scope: 'scope.userInfo'});
    }
    const userData=await promisic(wx.getUserProfile)({desc:'用于完善会员资料'});
    const data = await promisic(wx.login)();
    const nickName=userData.userInfo.nickName;
    const userImg=userData.userInfo.avatarUrl;
    const userGender=userData.userInfo.gender;
    const user_info = await userModel.userLogin(data.code,nickName,userImg,userGender);
    wx.setStorageSync("web_user_info", user_info);
    that.setData({
        loginShow: false
    });
    this.loadData(0,0);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const changeTypeLevel = wx.getStorageSync('changeTypeLevel');
    if(changeTypeLevel!=null){
        wx.setStorageSync('changeTypeLevel', null);
        const selData=changeTypeLevel.split(';');
        this.setData({
            selTypeMsg:selData[2],
            selTypeId:selData[0]
        });
        this.loadData(1,selData[0]);
    }else{
        this.loadData(0,0);
    }
  },
  //历年真题
  showYearTest:function(params) {
    wx.navigateTo({
        url: `/pages/past_year_list/index?courseId=${this.data.selTypeId}`
    });
  },
  //顺序练习
  sequentialExercise:function(){
    wx.navigateTo({
        url: `/pages/question/index?sourceType=2&typeId=0`
    });
  },
  //错题本
  errorBook:function(params) {
    wx.navigateTo({
        url: `/pages/error_book/index?courseId=${this.data.selTypeId}`
    });
  },
  //高频错题
  highErrorBook:function(params) {
    wx.navigateTo({
        url: `/pages/high_error_book/index?courseId=${this.data.selTypeId}`
    });
  },
  //收藏本
  collectBook:function(params) {
    wx.navigateTo({
        url: `/pages/collect_book/index?courseId=${this.data.selTypeId}`
    });
  },

   //我的笔记
   noteBook:function(params) {
    wx.navigateTo({
        url: `/pages/note_book/index?courseId=${this.data.selTypeId}`
    });
  },
  //每日一练
  dayPractice:function(params) {
    wx.navigateTo({
        url: `/pages/question/index?sourceType=7&typeId=0`
    });
  },
  //模拟考试
  mockExams:function(params){
    wx.navigateTo({
        url: `/pages/mock_index/index?courseId=${this.data.selTypeId}&courseName=${this.data.selTypeMsg}`
    });
  },
  //模考记录
  mockList:function (params) {
    wx.navigateTo({
        url: `/pages/mock_records/index?courseId=${this.data.selTypeId}`
    }); 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})